package com.ke.jdyd.presenter;

import com.ke.jdyd.http.BaseResponse;
import com.ke.jdyd.http.HttpResultObservable;
import com.ke.jdyd.service.LoginService;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoginPresenter {
  private LoginView mView;
  private LoginService loginService;

  public LoginPresenter(LoginView view) {
    this.mView = view;
    loginService = new LoginService();
  }
//P 相当一个中间人，用来协调view和server 的交互  view  <->  P <-> MODEL
    // MVP 和 MVC
    //MVP 和 MVVM

  public void login(String name, String pws) {
      //调用我们自己封装的网络请求提交数据
    loginService.loginIn(name, pws).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread(),false,100)
        .subscribe(new HttpResultObservable<LoginService.UserInfo>() {
          @Override
          public void onSuccess(LoginService.UserInfo result) {
            mView.loginSuccess(result);//调用View处理

          }

          @Override
          public void onSuccess(LoginService.UserInfo result, int code, String msg) {
            mView.loginFailed(msg);
          }

          @Override
          public void onFailed(String msg) {
            mView.loginFailed(msg);
          }
        });

  }

  public interface LoginView {
    void loginSuccess(LoginService.UserInfo token);

    void loginFailed(String msg);
  }
}
